package sale.wxb.loveshopping.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sale.wxb.loveshopping.entity.common.Result;
import sale.wxb.loveshopping.entity.model.CommodityClassify;
import sale.wxb.loveshopping.entity.vo.commodity.SaveCommodityClassifyVo;
import sale.wxb.loveshopping.entity.vo.common.PageListParam;
import sale.wxb.loveshopping.entity.vo.common.PageRecord;
import sale.wxb.loveshopping.service.ICommodityClassifyService;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@RequestMapping("commodity-classify")
@Api(tags = { "商品分类" })
@Validated
public class CommodityClassifyController {
    @Autowired
    private ICommodityClassifyService commodityClassifyService;

    @ApiOperation(value = "保存分类", notes = "传入ID则为更新否则为新增")
    @PostMapping("save")
    public Result<String> addCommodityClassify(@Validated @RequestBody SaveCommodityClassifyVo body) {
        return Result.success(commodityClassifyService.saveClassify(body).toString());
    }

    @ApiOperation("移除商品分类")
    @GetMapping("remove")
    public Result<?> removeCommodityClassifyById(@RequestParam @NotNull @Positive Long id) {
        return Result.success(commodityClassifyService.deleteClassify(id));
    }

    @ApiOperation("分类列表")
    @GetMapping("list")
    public Result<PageRecord<CommodityClassify>> list(@Validated PageListParam listParam) {
        return Result.success(commodityClassifyService.listByPage(listParam));
    }

    @ApiOperation("按id查询分类详情")
    @GetMapping("findById")
    public Result<CommodityClassify> findById(@RequestParam @NotNull @Positive Long id) {
        return Result.success(commodityClassifyService.getDetail(id));
    }
}
